
// user
var user_Boolean = false;
var password_Boolean = false;
var emaile_Boolean = false;
var Mobile_Boolean = false;
$('#reg_user').blur(function(){
    if ((/^[a-zA-Z0-9_-]{4,16}$/).test($("#reg_user").val())){
        $('.user_hint').html("✔").css("color","green");
        user_Boolean = true;
    }else {
        $('.user_hint').html("× 用户名无效").css("color","red");
        user_Boolean = false;
    }
});
// password
$('#reg_password').blur(function(){
    if ((/^[\w]{6,12}$/).test($("#reg_password").val())){
        $('.password_hint').html("✔").css("color","green");
        password_Boolean = true;
    }else {
        $('.password_hint').html("× 密码长度无效").css("color","red");
        password_Boolean = false;
    }
});

// Email
$('#reg_email').blur(function(){
    if ((/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/).test($("#reg_email").val())){
        $('.email_hint').html("✔").css("color","green");
        emaile_Boolean = true;
    }else {
        $('.email_hint').html("× 邮箱不正确").css("color","red");
        emaile_Boolean = false;
    }
});


// Mobile
$('#reg_mobile').blur(function(){
    if ((/^[1][3,4,5,7,8][0-9]{9}$/).test($("#reg_mobile").val())){
        $('.mobile_hint').html("✔").css("color","green");
        Mobile_Boolean = true;
    }else {
        $('.mobile_hint').html("× 手机号不正确").css("color","red");
        Mobile_Boolean = false;
    }
});

$(document).off("click",".register").on("click",".register",function(){
    if(user_Boolean && password_Boolean && emaile_Boolean && Mobile_Boolean == true) {

        var param = $('.registerFrm').serializeJson();
        var terms = "";
        if (Array.isArray(param.terms)) {
            console.log('Array')
            for (var i = 0; i < param.terms.length; i++) {
                if (i == param.terms.length - 1) {
                    terms += param.terms[i];
                } else {
                    terms += param.terms[i] + ',';
                }
            }
            param.terms = terms;
        }
        $.ajax({
            url: 'users/register',
            type: "get",
            data: param,
            async: true,
            success: function (res) {
                console.log(param);
                genRec(res.userId);
                setCookie('userId', res.userId);
                setCookie('username', param.user);
            }
        });
    }
    else {
        alert("请完善信息");
    }


});
$(document).off("submit",".registerFrm").on("submit",".registerFrm",function(){
    return false;
});
function logout() {
    $.cookie('userId', null);
    $.cookie('username', null);
    location.reload();
}


function genRec(userId) {
    $.ajax({
        url: 'http://120.78.183.13:8083',
        type: "get",
        data: {userId:userId},
        async: true,
        success: function (returnData) {
            console.log(returnData);
            window.location.href="index.html";
        },
        error: function (returnData) {
            console.log(returnData);
            setCookie('userId', userId);
            window.location.href="index.html";
        }
    });

}

//页面加载完毕执行
$(document).ready(function(){
    getRateBooks();
    randTerms();
    var username = getCookie('username');
    if (username != "null" && username != null) {
        $('#userName').text('你好'+username);
        $('#login').hide();
    } else
        $('#logout').hide()





});

function randTerms() {
    var terms = ['小说', '香港文学', '金融', '投资', '推理', '英国', '言情', '金钱', '理财', '散文', '日本'];
    $('.terms').empty();
    for (var i=0; i<3; i++) {
        var index = Math.floor((Math.random()*terms.length));
        var term = terms[index];
        console.log(term);
        terms.splice(index,1);
        $('.terms').append('<input type="checkbox" name="terms" value="'+term+'"/><label style="color: #000000;">'+term+'</label>')
    }
}

function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    // 30天过期
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}


function getRateBooks(){
    $.ajax({
        type: "get",
        url: "http://120.78.183.13/brs/book/randBooks",
        data: "",
        async: false,
        success:function (data) {
            $('.rand_books').empty();
            // totalPage = (data.bookCount + 6 - 1) / 6;
            res = data;
            var count = 0;
            for(each in data)
            {
                if (count === 3) {
                    break;
                }
                //    <div class="new_prod_box">
                //                             <a href="details.html">product name</a>
                //                             <div class="new_prod_bg">
                //                             <a href="details.html"><img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" /></a>
                //                             </div>
                //                         </div>
                var bookItem = '<div class="new_prod_box">\n' +
                    '                        <a href="details.html?bookId=BOOKID">BOOKNAME</a>\n' +
                    '                        <div class="new_prod_bg">\n' +
                    '                            <span class="new_icon"><img src="images/promo_icon.gif" alt="" title=""/></span>\n' +
                    '                            <a href="details.html?bookId=BOOKID"><img src="COVER" alt="" title="" class="thumb"\n' +
                    '                                                        border="0"/></a>\n' +
                    '                        </div>\n' +
                    '                    </div>';
                bookItem = bookItem.replace('BOOKNAME', data[each]['bookName']).replace(/BOOKID/g, data[each]['bookId']).replace('COVER', data[each]['bookCover']);
                $('.rand_books').append(bookItem);
                count++;
            }
        }
    });
}